<?php

namespace Frieza\Modules\Creators\Controllers;

use App\Exceptions\InputErrorException;
use Doraemon\model\Member;
use Doraemon\model\Task\TaskGroup as mTaskGroup;
use Doraemon\model\Task\UserTaskInfo;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Encore\Admin\Widgets\Form;
use Doraemon\pockets\excel\DownExcel;
use Illuminate\Support\MessageBag;

class ExportController
{
    public function index(Content $content, Request $request)
    {
        $content->header('限时任务导出')->description(' ');

        $form = new Form();
        $form->number('group_id', '任务组ID');
        $form->method('GET');
        $form->action('/admin/creators/export');
        $content->row($form);
        return $content;
    }

    public function export()
    {
        $param = request()->all();
        $groupId = $param['group_id'];
        try {
            if (empty($groupId)) {
                throw new \Exception('请传入任务组ID');
            }
            $taskGroup = mTaskGroup::getInfo(['id' => $groupId, 'cycle' => mTaskGroup::CYCLE_TYPE_LIMIT_TIME]);

            if (empty($taskGroup)) {
                throw new \Exception('查不到该任务组信息');
            }
            $userTaskInfo = UserTaskInfo::selectList(['info_type' => UserTaskInfo::INFO_TYPE_TASK, 'group_id' => $groupId]);

            if (empty($userTaskInfo)) {
                throw new \Exception('该任务组暂无用户完成信息');
            }
        } catch (\Exception $exception) {
            $error = new MessageBag(['title' => $exception->getMessage()]);
            return back()->with(compact('error'))->withInput();
        }

        $uid = array_column($userTaskInfo, 'uid');
        $userInfo = Member::getUserList($uid, 1);
        $data = [];
        foreach ($userTaskInfo as $value) {
            $data [] = [
                $groupId,
                $taskGroup['group_title'],
                $taskGroup['group_desc'],
                $userInfo[$value['uid']]['nick'] ?? '',
                $value['uid'],
                $userInfo[$value['uid']]['type_desc'] ?? '',
                $value['edittime'],
                $value['finished_num'] . '/' . $value['total'],
            ];
        }
        $downExcel = new DownExcel();
        $letter = range('A', 'H');
        $widths = ['A' => 10, 'B' => 15, 'C' => 25, 'D' => 25, 'E' => 15, 'F' => 20, 'G' => 25,'H'=>10];
        $title = $taskGroup['group_title'] . '-' . $taskGroup['group_desc'] . '学员信息查询';
        $downExcel->setFrontBold('1');
        $downExcel->setWidth($widths);
        $downExcel->setFileName($title);
        $downExcel->setTitle($title);
        $downExcel->setSheetTitle($title);

        $header = ['任务组ID', '任务组ID', '任务组描述', '用户昵称', '用户UID', '用户身份','任务完成时间', '任务进度'];
        foreach ($header as $key => $value) {
            //设置字体
            $downExcel->setFrontName($letter[$key]);
            //设置大小
            $downExcel->setFrontSize($letter[$key]);
        }
        $downExcel->create($data, $header);
        return back();
    }
}